package de.hwrberlin.remo.export.prolog;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

public class PrologExportDialog extends TitleAreaDialog {

	private Button checkExportKnowledgeBase;
	private Button checkExportFacts;
	private Button buttonKnowledgeBaseFile;
	private Button buttonFactFile;

	private Label labelFactFile;
	private Label labelKnowledgeBaseFile;
	private Label labelCaseName;
	
	private Text textKnoledgeBaseFile;
	private Text textFactFile;
	private Text textCaseName;
	
	private boolean exportKnowledgeBase = false;
	private boolean exportFacts = false;
	
	private String knowledgeBaseFile;
	private String factFile;
	private String caseName;
	
	public PrologExportDialog(Shell parentShell) {
		super(parentShell);
	}
	
	//////////////////
	/* GUI creation */

	@Override
	protected Control createDialogArea(Composite parent) {
		setTitle("Prolog Export");
		
		Composite parentComposite = (Composite) super.createDialogArea(parent);
		FormToolkit toolkit = new FormToolkit(parentComposite.getDisplay());
		
		Section optionSection = toolkit.createSection(parentComposite, Section.TITLE_BAR);
		optionSection.setText("Export Options");
		
		Composite optionSectionClient = toolkit.createComposite(optionSection);
		optionSectionClient.setLayout(new GridLayout());
		createKnowledgeBaseExportOptions(toolkit, optionSectionClient);
		createFactExportOptions(toolkit, optionSectionClient);
		
		optionSection.setClient(optionSectionClient);
		GridDataFactory gdf = GridDataFactory.fillDefaults().grab(true, true);
		gdf.applyTo(optionSection);
		gdf.applyTo(optionSectionClient);

		initializeMessage();
		
		return parentComposite;
	}
	
	private void createKnowledgeBaseExportOptions(FormToolkit toolkit, Composite parent) {
		Composite knowledgeBaseExportComposite = toolkit.createComposite(parent);
		knowledgeBaseExportComposite.setLayout(new GridLayout(4, false));
		GridDataFactory.fillDefaults().grab(true, false).applyTo(knowledgeBaseExportComposite);
		
		checkExportKnowledgeBase = toolkit.createButton(knowledgeBaseExportComposite,"", SWT.CHECK);
		checkExportKnowledgeBase.setSelection(exportKnowledgeBase);
		checkExportKnowledgeBase.addSelectionListener(checkBoxSelelectionListener);
		
		Label label = toolkit.createLabel(knowledgeBaseExportComposite, "Export Knowledge Base", SWT.NONE);
		GridDataFactory.fillDefaults().span(3, 1).applyTo(label);
		
		// Create dummy label to occupy first column
		toolkit.createLabel(knowledgeBaseExportComposite, "");
		labelKnowledgeBaseFile = toolkit.createLabel(knowledgeBaseExportComposite, "File:");
		labelKnowledgeBaseFile.setEnabled(exportKnowledgeBase);
		
		textKnoledgeBaseFile = toolkit.createText(knowledgeBaseExportComposite,"", SWT.READ_ONLY);
		textKnoledgeBaseFile.setEnabled(exportKnowledgeBase);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(textKnoledgeBaseFile);
		
		buttonKnowledgeBaseFile = toolkit.createButton(knowledgeBaseExportComposite, "Browse", SWT.PUSH);
		buttonKnowledgeBaseFile.setEnabled(exportKnowledgeBase);
		buttonKnowledgeBaseFile.addSelectionListener(buttonSelectionListener);
	}
	
	private void createFactExportOptions(FormToolkit toolkit, Composite parent) {
		Composite factExportComposite = toolkit.createComposite(parent);
		factExportComposite.setLayout(new GridLayout(4, false));
		GridDataFactory.fillDefaults().grab(true, false).applyTo(factExportComposite);
		
		checkExportFacts = toolkit.createButton(factExportComposite,"", SWT.CHECK);
		checkExportFacts.setSelection(exportFacts);
		checkExportFacts.addSelectionListener(checkBoxSelelectionListener);
		
		Label label = toolkit.createLabel(factExportComposite, "Export Facts", SWT.NONE);
		GridDataFactory.fillDefaults().span(3, 1).applyTo(label);

		// Create dummy label to occupy first column
		toolkit.createLabel(factExportComposite, "");
		labelFactFile = toolkit.createLabel(factExportComposite, "File:");
		labelFactFile.setEnabled(exportFacts);
		
		textFactFile = toolkit.createText(factExportComposite,"", SWT.READ_ONLY);
		textFactFile.setEnabled(exportFacts);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(textFactFile);
		
		buttonFactFile = toolkit.createButton(factExportComposite, "Browse", SWT.PUSH);
		buttonFactFile.addSelectionListener(buttonSelectionListener);
		buttonFactFile.setEnabled(exportFacts);
		
		// Create dummy label to occupy first column
		toolkit.createLabel(factExportComposite, "");
		labelCaseName = toolkit.createLabel(factExportComposite, "Case:");
		labelCaseName.setEnabled(exportFacts);
		
		textCaseName = toolkit.createText(factExportComposite, "");
		textCaseName.setEnabled(exportFacts);
		GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(textCaseName);
	}

	@Override
	protected void okPressed() {
		exportKnowledgeBase = checkExportKnowledgeBase.getSelection();
		exportFacts = checkExportFacts.getSelection();
		
		knowledgeBaseFile = textKnoledgeBaseFile.getText();
		factFile = textFactFile.getText();
		caseName = textCaseName.getText();
		
		if(exportKnowledgeBase && (knowledgeBaseFile == null || knowledgeBaseFile.length() == 0)) {
			setMessage("Please select an knowlegde base file.", IMessageProvider.ERROR);
			return;
		}
		
		if(exportFacts && (factFile == null || factFile.length() == 0)) {
			setMessage("Please select an fact file.", IMessageProvider.ERROR);
			return;
		}
		
		if(exportFacts && (caseName == null || caseName.length() == 0)) {
			setMessage("Please enter an case name.", IMessageProvider.ERROR);
			return;
		}
		
		super.okPressed();
	}
	
	@Override
	protected void cancelPressed() {
		exportKnowledgeBase = false;
		exportFacts = false;
		
		knowledgeBaseFile = null;
		factFile = null;
		caseName = null;
		
		super.cancelPressed();
	}
	
	private void initializeMessage() {
		setMessage("Specify the export options", IMessageProvider.INFORMATION);
	}
	
	/////////////////////
	/* Getter / Setter */
	
	public boolean isExportKnowledgeBase() {
		return exportKnowledgeBase;
	}

	public boolean isExportFacts() {
		return exportFacts;
	}

	public String getKnowledgeBaseFile() {
		return knowledgeBaseFile;
	}

	public String getFactFile() {
		return factFile;
	}	
	
	public String getCaseName() {
		return caseName;
	}
	
	////////////////////////
	/* Selection listener */
	
	private SelectionListener checkBoxSelelectionListener = new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			if(e.widget == checkExportKnowledgeBase) {
				boolean enabled = checkExportKnowledgeBase.getSelection();
				textKnoledgeBaseFile.setEnabled(enabled);
				labelKnowledgeBaseFile.setEnabled(enabled);
				buttonKnowledgeBaseFile.setEnabled(enabled);
				
			} else if(e.widget == checkExportFacts) {
				boolean enabled = checkExportFacts.getSelection();
				textFactFile.setEnabled(enabled);
				labelFactFile.setEnabled(enabled);
				buttonFactFile.setEnabled(enabled);
				
				labelCaseName.setEnabled(enabled);
				textCaseName.setEnabled(enabled);
			}
		};
	};

	private SelectionListener buttonSelectionListener = new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
			String selectedFile = dialog.open();
			
			if(selectedFile != null) {
				if(e.widget == buttonKnowledgeBaseFile) {
					textKnoledgeBaseFile.setText(selectedFile);
				
				} else if(e.widget == buttonFactFile) {
					textFactFile.setText(selectedFile);
				}
			}
		};
	};
}